/**
 * 代理人管理管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：代理人管理
     创建日期：2015-10-15
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	agentList();
});


/**
 * 查询代理人列表
 * @returns
 */
var agentList = function(){
	var headPath= phpHeadPath;
	var agentNum=$("#agentNum").val();
	var agentName=$("#agentName").val();
	var agentPhone=$("#agentPhone").val();
	var agentAdress=$("#agentAdress").val();
	var datas =  {"p":"agentManageOperate","m":"query","agentNum":agentNum,"agentName":agentName,"agentPhone":agentPhone,"agentAdress":agentAdress,"type":1};
	var columnDefine = {"id":"编号","name":"代理人名称","phone":"联系电话","address":"详细地址","qrcode":"二维码","newUser":"新增用户/快递员"};
	
	var formatters={
	            "qrcode": function (column, rowData)
	            {
	            	if(rowData.qrcode==''||rowData.qrcode==null){
	        			return "";
	        		}
	        		return '<a style="width:50px;height:50px;display:block;background:url('+headPath+'/'+rowData.qrcode+') no-repeat;background-size:50px 50px; "></a>';
	            },
	            "createDate": function (column, rowData)
	            {
	            	return row.useDate;
	            },
	            "status": function (column, rowData)
	            {
    				return rowData.status == 0 ?'未使用':'已使用';
	            }
	 };
	 var buttons ='<script type="text/javascript">showBigQrCode();</script>'; //操作按钮
		buttons+='<p>';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showDevelopUser();">查看推荐用户</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="showModifyWin();">编辑</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-warning\' onclick="settlement();">结算</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="createQrCode();">生成二维码</button>&nbsp;&nbsp;<br>';
		buttons+='</p>';
	   hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
	
};

/**
 * 显示大图
 * @param imgUrl
 * @returns
 */
var showBigQrCode = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#pop_win .modal-body').append('<img alt="" src="'+ phpHeadPath+'/'+json.qrcode+'" >');
};

/**
 * 生成二维码
 * @returns
 */
var createQrCode = function(){
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	
	var datas={"p":"createAgentHtml","m":"code","id":json.id,"type":1};
	var rjson=common.ajaxRequest('service' , datas, "post");
	if(rjson.result=="0")
	{
		agentList();
		$('#pop_win').modal('hide');
		common.showMsg('info', '', '生成二维码成功!');
	}
	else
	{
		common.showMsg('error', '', '生成二维码失败!');
	}
};

/**
 * 查看用户
 * @returns
 */
var showDevelopUser = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	window.location.href='page?p=page/showAgentDevelop&agentId='+json.id ;
};

/**
 * 打开新建代理人窗口
 * @returns
 */
var showAddAgentWin = function(){
	$('#newAgentWin input').val('');
	$('#newAgentWin').modal('show');
};

/**
 * 打开编辑窗口
 * @returns
 */
var showModifyWin = function(){
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#modifyAgentWin .id').val(json.id);
	$('#modifyAgentWin .agentName').val(json.name);
	$('#modifyAgentWin .tel').val(json.phone);
	$('#modifyAgentWin .address').val(json.address);
	$('#pop_win').modal('hide');
	$('#modifyAgentWin').modal('show');
};


/**
 * 修改代理人
 * @returns
 */
var modifyAgent = function(){
	
	var agentId = $('#modifyAgentWin .id').val();
	var agentName = $('#modifyAgentWin .agentName').val();
	var agentTel = $('#modifyAgentWin .tel').val();
	var agentAddress = $('#modifyAgentWin .address').val();
	var datas = {"p":"agentManageOperate","m":"edit","id":agentId,"name":agentName,"phone":agentTel,"address":agentAddress,"type":1};

	var json = common.ajaxRequest('service', datas, 'POST');
	if( json && json.result == 0 )
	{
		agentList();
		$('#modifyAgentWin').modal('hide');
		common.showMsg('info', '', '修改成功!');
	}
	else
	{
		common.showMsg('error', '', json.message);
	}	
};

/**
 * 新建代理人
 * @returns
 */
var addAgent = function(){
	
	var agentName = $('#newAgentWin .agentName').val();
	var agentTel = $('#newAgentWin .tel').val();
	var agentAddress = $('#newAgentWin .address').val();
	var datas = {"p":"agentManageOperate","m":"add","name":agentName,"phone":agentTel,"address":agentAddress,"type":1};

	var json = common.ajaxRequest('service', datas, 'POST');
	if( json && json.result == 0 )
	{
		agentList();
		$('#newAgentWin').modal('hide');
		common.showMsg('info', '', '新建成功!');
	}
	else
	{
		common.showMsg('error', '', json.message);
	}	
	
};

/**
 * 结算
 * @returns
 */
var settlement = function(){
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	var num = json.newUser;
	var id = json.id;
	
	if(num==0){
		common.showMsg('error', '', '新推荐用户数为零!');
		return;
	}
	var datas={"p":"createAgentHtml","m":"js","id":agentId};
	var rjson=common.ajaxRequest('service', datas, "POST");
	if(rjson.result=="0")
	{
		agentList();
		$('#pop_win').modal('hide');
		common.showMsg('info', '', '结算成功!');
	}
	else
	{
		common.showMsg('error', '', '结算失败!');
	}
};